package com.example.administrator.phpercool;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

//import com.tencent.stat.StatConfig;
//import com.tencent.stat.StatService;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private SimpleAdapter simpleAdapter;
    private ArrayList<Map<String,Object>> data;
    private Map<String,Object> map;

    private InputStreamReader inputStreamReader;
    private BufferedReader bufferedReader;
    private StringBuilder stringBuilder;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // [可选]设置是否打开debug输出，上线时请关闭，Logcat标签为"MtaSDK"
        //StatConfig.setDebugEnable(true);
        // 基础统计API
        //StatService.registerActivityLifecycleCallbacks(this.getApplication());


        BottomNavigationView navigationView = findViewById(R.id.navigation);
        navigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        setListView("line.json");
        //TextView textView = findViewById(R.id.textView);


    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_line:
                    //基础
                    setListView("line.json");
                    return true;
                case R.id.navigation_top:
                    //提升
                    setListView("top.json");
                    return true;
                case R.id.navigation_skills:
                    //技巧
                    setListView("skills.json");
                    return true;
                case R.id.navigation_fun:
                    //函数
                    Intent intent = new Intent(MainActivity.this, FunctionActivity.class);
                    startActivity(intent);
                    return true;
            }
            return false;
        }
    };

    private void setListView(String str){
        //获取xml控件id
        listView = findViewById(R.id.listView);

        //初始化数据源
        data = new ArrayList<Map<String,Object>>();

        try {
            //读取文件，字节流转 字符串
            inputStreamReader = new InputStreamReader(getAssets().open(str),"utf-8");
            //字符缓冲，提升速度
            bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            //定义可变字符变量
            stringBuilder = new StringBuilder();
            while ( (line = bufferedReader.readLine()) != null){
                stringBuilder.append(line);
            }
            bufferedReader.close();
            inputStreamReader.close();

            try {
                //字符串转换json对象
                JSONObject jsonObject = new JSONObject(stringBuilder.toString());
                ////从JSONObject中取出数组对象
                JSONArray jsonArray = jsonObject.getJSONArray("key");

                for (int i=0;i<jsonArray.length();i++){
                    JSONObject article_list = jsonArray.getJSONObject(i);//取出数组中的对象
                    map = new HashMap<String, Object>();
                    map.put("_id",article_list.getInt("id")); //取出数组中对象的各个值
                    map.put("title",article_list.getString("post_title"));
                    map.put("post_content",article_list.getString("post_content"));
                    data.add(map);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


        String[] from = {"title"};
        int[] to = {R.id.title};

        simpleAdapter = new SimpleAdapter(MainActivity.this,data,R.layout.simpleadapter_item,from,to);
        //赋值
        listView.setAdapter(simpleAdapter);
        //监听单击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //1.获取listview 里的值
                //TextView  textView = view.findViewById(R.id._id);
                //String _id = textView.getText().toString();
                //Toast.makeText(MainActivity.this,"heheh"+_id,Toast.LENGTH_LONG).show();


                //2.获取listview 里的值
                //Map item = (Map)parent.getItemAtPosition(position);
                //Toast.makeText(MainActivity.this,"heheh"+item.get("post_content"),Toast.LENGTH_LONG).show();

                Map item = (Map)parent.getItemAtPosition(position);
                Intent intent = new Intent(MainActivity.this,ArticleActivity.class);
                intent.putExtra("post_content",item.get("post_content").toString());
                startActivity(intent);
            }
        });
    }
}
